草庐IT

C++11 auto 和 size_type

全部标签

C++ : Different deduction of type auto between const int * and cont int &

这里是代码示例。a.intii=0;b.constintci=ii;c.autoe=&ci;-->eisconstint*d.auto&f=42;-->invalidinitializationofnon-constreferenceoftype‘int&’fromanrvalueoftype‘int’e.constauto&g=42-->ok观察:1.对于c)子句,自动推导类型const2.对于子句d),不会自动推导出类型const3.对于条款e),必须手动添加类型const才能使其工作。为什么子句c而不是d会自动推导类型const? 最佳答案

c++ - 导致 C++11 std::mutex 将阻塞的线程锁定为被动等待状态?

我有以下情况:两个C++11线程正在计算,它们通过std::mutex同步。线程A锁定互斥锁,直到数据准备好供线程B执行的操作使用。当互斥量解锁时,线程B开始工作。线程B试图锁定互斥量并被阻塞,直到它被线程A解锁。voidThreadA(std::mutex*mtx,char*data){mtx->lock();//dosomethingusefulwithdatamtx->unlock();}voidThreadB(std::mutex*mtx,char*data){mtx->lock();//waituntilThreadAisready//dosomethingusefulwit

c++ - C++11 中的静态局部变量?

有什么区别:classA{public:staticconstA&GetInstance(){staticAa;returna;}};和classB{public:staticconstB*GetInstance(){staticB*b=newB;returnb;}};?A和B之间的Singleton的生命周期是否存在差异?对象的内存位置?一般有什么区别吗? 最佳答案 这两种情况下对象的生命周期是不同的。C++保证静态局部对象将以与其构造相反的顺序销毁。在这两种情况下,构造都将在首次调用GetInstance时发生。但是,在第二种情

c++ - 'for(auto &str : vec)' 内部 for 循环的目的是什么?

我是C++的新手,正在尝试学习vector的概念。我在网上看到这段代码。我的问题是,'for(auto&str:vec)'中的内部for循环的目的是什么?为什么作者要对第一个引用(&str)创建第二个引用(&c)?intmain(){vectorvec;for(stringword;cin>>word;vec.push_back(word)){}for(auto&str:vec){for(auto&c:str){c=toupper(c);}}for(inti=0;i!=vec.size();++i){if(i!=0&&i%8==0)cout 最佳答案

c++ - 如何在 C++11 中初始化 std::vector 的值列表?

我对以下代码有疑问:conststd::vectorarr1={"a","b","c"};conststd::vectorarr2={"e","f","g"};conststd::vectorglobaArr={arr1,arr2};//error我需要用以下值初始化globalArr:“a”、“b”、“c”、“e”、“f”、“g”(一维)。我不需要二维数组。我做错了什么?我可以这样做:globalArr.push_back(arr1);//withtheforloopinsertingeachvalueofarr1globalArr.push_back(arr2);但这里的globa

C++11 constexpr 导致编译器的内部错误 (C1001)

我正在使用VisualStudio2015Update3。我得到一个fatalerror:(codeC1001):Aninternalerrorhasoccurredinthecompiler.代码如下:templateconstexprTepsilon=std::numeric_limits::epsilon();我读到它已在VisualStudioUpdate2中修复。有人可以解释我为什么会收到此错误吗?提前致谢。 最佳答案 任何内部错误(ICE)都是编译器错误。你得到它是因为你碰巧触发了那个错误。对于此编译器,您可以在Micr

C++11 函数局部静态常量对象的线程安全初始化

此问题已在C++98上下文中提出,并在该上下文中得到回答,但没有明确说明C++11constsome_type&create_const_thingy(){lockmy_lock(some_mutex);staticconstsome_typethe_const_thingy;returnthe_const_thingy;}voiduse_const_thingy(){staticconstsome_type&the_const_thingy=create_const_thingy();//usethe_const_thingy}这个初始化模式会确保:没有出现竞争条件create_co

c++ - 带有 if 语句的 auto 函数不会返回值

我制作了一个模板和一个auto函数,用于比较2个值并返回最小值。这是我的代码:#includeusingnamespacestd;//Templatewithavaluereturningfunction:PrintSmallertemplateautoPrintSmaller(TNumOne,UNumTwo){if(NumOne>NumTwo){returnNumTwo;}else{returnNumOne;}}intmain(){intiA=345;floatfB=23.4243;cout但它无法编译,我在VS2015上遇到此错误:错误C3487“int”:所有返回表达式必须推导出

c++ - 循环依赖 : can't delete an incomplete type

我不明白为什么会出现此编译器错误:errorC2027:useofundefinedtype'GameState'note:seedeclarationof'GameState'errorC2338:can'tdeleteanincompletetypewarningC4150:deletionofpointertoincompletetype'GameState';nodestructorcalled这是相关代码:#pragmaonce#include#include"SpawnManager.h"#include"Resource.h"#include#includeclassGa

C++11:atomic<T>::store 和 atomic_store<T> 之间有什么区别

一个是模板类std::atomic的成员函数,一个是模板函数,看起来他们做的是同一件事。既然std是一个类库,为什么它同时提供类和非类版本,我认为是一样的操作?它们之间有什么真正的区别吗? 最佳答案 语义上没有区别。免费功能是为了实现与C11的源代码兼容性的尝试:#ifdef__cplusplus#include#define_Atomic(X)std::atomic#else#include#endif_Atomic(int)c;intget_c(void){returnatomic_load(&c);}